/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.user.client.ui.nojs;

import com.cleangwt.bootstrap.client.style.Styles;
import com.cleangwt.user.client.ui.Button;
import com.cleangwt.user.client.ui.RawWidget;
import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.dom.client.ButtonElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * The dialog box to replace com.google.gwt.user.client.ui.DialogBox.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 * @deprecated experimatal
 */
public class DialogBox {

  /**
   * A interface defines an action {@link #execute(String)} to be invoked on {@link DialogBox}
   * closing.
   */
  public interface OptionCommand {
    void execute(String option);
  }

  private static final String DIALOG_STYLE = "modal-dialog";
  private static final String BODY_STYLE = "modal-body";
  private static final String CONTENT_STYLE = "modal-content";
  private static final String FOOTER_STYLE = "modal-footer";
  private static final String HEADER_STYLE = "modal-header";
  private static final String TITLE_STYLE = "modal-title";

  private Element bodyElement = Document.get().createDivElement();
  private OptionCommand callback;
  /** The handler for option button */
  private ClickHandler optionCallbackHandler = new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {
      close();
      if (callback != null) {
        ButtonElement e = event.getRelativeElement().cast();
        if (!e.getValue().isEmpty()) {
          callback.execute(e.getValue());
        }
      }
    }
  };
  private Element footerElement = Document.get().createDivElement();
  private Element headerElement = Document.get().createDivElement();
  private HTMLPanel pnlContent = new HTMLPanel("");
  private PopupPanel popup;

  private Element titleElement = Document.get().createHElement(4);

  /**
   * Creates an dialog box with the given 'modal'.
   * 
   * @param modal the modal state
   */
  public DialogBox(boolean modal) {
    this(modal, false);
  }

  /**
   * Creates an dialog box with the given 'modal' and 'glassEnabled'.
   * 
   * @param modal the modal state
   * @param glassEnabled the glass enabled
   */
  public DialogBox(boolean modal, boolean glassEnabled) {
    // no auto hide
    this.popup = new PopupPanel(false, modal);
    popup.setStyleName(DIALOG_STYLE);
    popup.setGlassEnabled(glassEnabled);
    RawWidget close = new RawWidget(AnchorElement.TAG, "&times;");

    pnlContent.getElement().appendChild(headerElement);
    pnlContent.getElement().appendChild(bodyElement);
    pnlContent.getElement().appendChild(footerElement);
    pnlContent.setStyleName(CONTENT_STYLE);
    pnlContent.add(close, headerElement);

    headerElement.appendChild(titleElement);
    headerElement.setClassName(HEADER_STYLE);
    titleElement.setClassName(TITLE_STYLE);
    bodyElement.setClassName(BODY_STYLE);
    footerElement.setClassName(FOOTER_STYLE);

    popup.setWidget(pnlContent);
    popup.setAnimationEnabled(true);
    popup.setAutoHideOnHistoryEventsEnabled(true);

    close.setStyleName(Styles.CLOSE);
    close.addDomHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        close();
      }
    }, ClickEvent.getType());
  }

  /**
   * Adds a cancel option.
   * 
   * @param html the option text
   */
  public Button addCloseOption(String html) {
    Button btn = new Button();
    btn.setHTML(html);
    addOption(btn, null);
    return btn;
  }

  /**
   * Adds an option.
   * 
   * @param text the option text
   * @param optionValue the option value
   */
  public void addOption(Button btn, final String optionValue) {
    // Button btn = new Button(text);
    if (optionValue != null) {
      btn.getElement().setAttribute("value", optionValue);
    }
    pnlContent.add(btn, footerElement);
    btn.addClickHandler(optionCallbackHandler);
  }

  /**
   * Closes the dialog box.
   */
  public void close() {
    popup.hide();
  }

  /**
   * Sets body content with the given html.
   * 
   * @param html the html
   */
  public void setBody(String html) {
    this.bodyElement.setInnerHTML(html);
  }

  /**
   * Sets body content with the given widget.
   * 
   * @param widget the widget
   */
  public void setBody(Widget widget) {
    this.pnlContent.add(widget, bodyElement);
  }

  /**
   * Sets the callback
   * 
   * @param callback the callback
   */
  public void setCallback(OptionCommand callback) {
    this.callback = callback;
  }

  /**
   * Sets the title.
   * 
   * @param title the title
   */
  public void setTitle(String title) {
    titleElement.setInnerHTML(title);
  }

  /**
   * Shows the dialog box.
   */
  public void show() {
    popup.center();
  }

}
